/**
 * Created by Luji on 2017/02/03.
 */
import delTmp from './form/delete.html';
import delCtrl from './form/delete.ctrl.js';
class App {
    constructor(list, appSrv, ctgNotification,$uiModal) {
        "ngInject";
        this._appSrv       = appSrv;
        this._notification = ctgNotification;
        this._uiModal      = $uiModal;
        this.table         = [];
        this.page = {
            currentPage: 1,
            totalItems: 0
        };
        this.filter = {
            name: null
        };

        if(list){
            this.table = list.list;
            this.page.totalItems = list.total;
        }
    }
    search() {
        this.page.currentPage = 1;
        this.getList();
    }
    getList(){
        this.table = null;

        this._appSrv.getSerDepList({currentPage: this.page.currentPage}, this.filter).then(data=> {
            this.table = data.list;
            this.page.totalItems = data.total;
        }).catch(()=> {
            this.table = [];
            this.page.totalItems = 0;
            this._notification.error('获取服务依赖数据有误.');
        });
    }

    deleteSer(elem) {
        this._uiModal.open({
            templateUrl: delTmp,
            controller: delCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                alert: function () {
                    return {
                        name: elem.name
                    }
                }
            }
        }).result.then(()=> {
                elem.deleting = true;
                this._appSrv.deleteSer(elem.id).then(data=> {
                    this._notification.success('删除服务依赖 ' + elem.name + ' 成功.');
                    this.getList();
                }).catch(data=> {
                    this._notification.error('删除服务依赖' + elem.name + ' 失败.');
                }).finally(()=> {
                    elem.deleting = false;
                })
            });
    }

}
export default App